package com.ipoint.icompany.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import com.ipoint.icompany.model.Day;

public class DayGenerator {
	private int year;

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public DayGenerator(int year) {
		super();
		this.year = year;
	}

	public List<Day> generateDaysForYear() {
		List<Day> result = new ArrayList<Day>(getMaxDayOfYear());
		for (int i = 1; i <= getMaxDayOfYear(); i++) {
			result.add(generateDay(i));
		}
		return result;
	}

	private int getMaxDayOfYear() {
		Calendar calendar = GregorianCalendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		return calendar.getActualMaximum(Calendar.DAY_OF_YEAR);
	}

	private Day generateDay(int dayOfYear) {
		Day result = new Day();
		Calendar calendar = getCalendarForDayOfYear(dayOfYear);
		result.setDate(calendar.getTime());
		result.setWorking(calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY
				&& calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY);
		return result;
	}

	private Calendar getCalendarForDayOfYear(int dayOfYear) {
		Calendar calendar = GregorianCalendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.DAY_OF_YEAR, dayOfYear);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar;
	}

}
